package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.append.DeleteUserAppendBo;
import com.zhuss.bean.bo.core.append.wechat.FocusUserBo;
import com.zhuss.bean.vo.core.append.DetailUserAppendVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 取消关注用户 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("userDelFocus")
public class UserDelFocus extends AbstractProcessor<Object, FocusUserBo> implements Processor<Object, FocusUserBo> {

    @Autowired
	@Qualifier("userAppendRead")
	private UserAppendRead userAppendRead;
    
    @Autowired
    @Qualifier("userAppendDelete")
    private Processor<Object, DeleteUserAppendBo> userAppendDelete;
    
    /**
     * 校验
     */
    @Override
    public void check(FocusUserBo bo) throws WpsException {
    	Long userId = bo.getUserId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailUserAppendVo result = userAppendRead.getByType(appendType, userId, bo.getConcurrentUserId());
    	
    	if (result == null) {
    		throw new WpsException(ExceptionCode.用户未关注);
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(FocusUserBo param) throws WpsException {
    	Long userId = param.getUserId();
    	Integer appendType = param.getAppendType();
    	
    	DetailUserAppendVo result = userAppendRead.getByType(appendType, userId, param.getConcurrentUserId());
    	
    	DeleteUserAppendBo bo = new DeleteUserAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	bo.setUserAppendId(result.getUserAppendId());
    	userAppendDelete.process(bo);
    	
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(FocusUserBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.黑名单.key());
    	record.setAction(LogActionEnum.用户取消关注.key());
    	record.setTargetId(param.getUserId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
